/**
 * 
 */
package la.ge.server.servlets.repository.viewer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import la.ge.kml.ErrorReportKMLBuilder;
import la.ge.kml.KMLConstants;

import org.apache.commons.io.IOUtils;

@SuppressWarnings("serial")
public class RespositoryViewerServlet extends HttpServlet
{
	public RespositoryViewerServlet() {
	}
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
    	String filename = request.getParameter( "file" );
    	File file = new File("./repository/" + filename);
		response.setContentType( KMLConstants.KML_MIME );
		response.setStatus(HttpServletResponse.SC_OK);
		
		InputStream is = null;
		try {
			is = new FileInputStream( file );
			IOUtils.copy( is, response.getOutputStream() );
		} catch (IOException e) {
			ErrorReportKMLBuilder builder = new ErrorReportKMLBuilder();
			response.getWriter().append( builder.build( e ) );
		}
		finally {
			IOUtils.closeQuietly( is );
		}
    }
}